package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Massage;
import com.qf.model.Navig;
import com.qf.model.PageBean;
import com.qf.model.pojo.UserCard;
import com.qf.service.NavigService;
import com.qf.service.UserCardService;
import com.qf.service.impl.NavigServiceImpl;
import com.qf.service.impl.UserCardServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/UserCardServlet")
public class UserCardServlet extends BascServlet{
    UserCardService userCardService = new UserCardServiceImpl();
    //查询是否具有审批后的信用卡,用于还款的操作
    protected void findCreditCardByU_id(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取用户id
        int u_id = Integer.parseInt(req.getParameter("userid"));
        //调用方法取查询所有的审批后的信用卡
        List<UserCard> list = userCardService.findCreditCardByU_id(u_id);
        Massage<List<UserCard>> m=null;
        //判断查询结果
        if(list.size() > 0){
            m = new Massage<>(1,"用户存在可用的信用卡",list);
        }else {
            /*m = new Massage<>(-1,"用户还没有信用卡,是否取申请信用卡");*/
            m = new Massage<>(-1,"您还没有信用卡,请先申请信用卡");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }
    //注册提交
    protected void applyCreditCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取用户身份证号
        String u_idcard = req.getParameter("u_idcard");
        //获取开户行
        String c_account = req.getParameter("c_account");//-1:未选择开户行 1:中国银行 2:中国邮政 3:中国农业银行
        //获取手机号
        String c_phone = req.getParameter("u_phone");
        //获取验证码
        String code = req.getParameter("code");
        //获取用户姓名
        String u_name = req.getParameter("u_name");
        //获取用户id
        int u_id = Integer.parseInt(req.getParameter("u_id"));
        //判断验证码,从session域中取到验证码
        String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);

        Massage m = null;
        if(!attribute.equals(code)){
            //添加申请信息
            m = new Massage<>(-1,"验证码错误");
        }else if(c_account.equals("-1")){
            m = new Massage<>(-2,"未选择开户行");
        }else {
            //调用方法
            String a="";
            if(c_account=="1"){
                a="中国银行";
            }else if(c_account=="2"){
                a="中国邮政";
            }else {
                a="中国农业银行";
            }
            int update = userCardService.applyCreditCard(u_idcard,u_name,a,c_phone,u_id);
            if(update == 1){
                m = new Massage(1,"信用卡申请成功,审核时间为两个工作日以内，请您耐心等待");
            }else {
                m = new Massage(-3,"信用卡申请失败！");
            }
        }

        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);

    }
    //查询所有审核银行卡
    public  void  findAll(HttpServletRequest req, HttpServletResponse resp)throws  Exception{
        //page:page,pagesize:pagesize
        //获取前端传入的数据
        int page=Integer.parseInt(req.getParameter("page"));
        int pagesize=Integer.parseInt(req.getParameter("pagesize"));

        PageBean<UserCard> pageBean=new PageBean<>();
        pageBean.setCurPage(page);
        pageBean.setPageSize(pagesize);


        List<UserCard> userCards = userCardService.findAll(pageBean);

        pageBean.setData(userCards);

        if(pageBean.getCount()%pageBean.getPageSize()==0){
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize());
        }else {
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize()+1);
        }

        //创建返回信息对象
        Massage< PageBean<UserCard>> m=new Massage<>(1,"查询成功",pageBean);
        //转换成json格式
        String s = JSON.toJSONString(m);
        //响应给前端
        resp.getWriter().write(s);
    }


}
